 package server.reward;

 import client.MapleCharacter;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Timestamp;
 import java.util.LinkedList;
 import java.util.List;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.packet.RewardPacket;


 public class StarReward
 {
/*  19 */   public static final Logger log = LogManager.getLogger(StarReward.class);

   private int id;
   private int itemid;
   private int quantity;
/*  24 */   private int unk = 0;

   private int period;
   private Timestamp startTime;
   private Timestamp endTime;
   private String msg;
/*  30 */   private static final StarReward instance = new StarReward();

   public static StarReward getInstance() {
/*  33 */     return instance;
   }


   public StarReward() {}


   public StarReward(int itemid, int quantity, int period, Timestamp startTime, Timestamp endTime, String msg) {
/*  41 */     this.itemid = itemid;
/*  42 */     this.quantity = quantity;
/*  43 */     this.period = period;
/*  44 */     this.startTime = startTime;
/*  45 */     this.endTime = endTime;
/*  46 */     this.msg = msg;
   }

   public StarReward(int id, int itemid, int num, int unk, int period, Timestamp startTime, Timestamp endTime, String msg) {
/*  50 */     this.id = id;
/*  51 */     this.itemid = itemid;
/*  52 */     this.quantity = num;
/*  53 */     this.unk = unk;
/*  54 */     this.period = period;
/*  55 */     this.startTime = startTime;
/*  56 */     this.endTime = endTime;
/*  57 */     this.msg = msg;
   }

   public int getId() {
/*  61 */     return this.id;
   }

   public void setId(int id) {
/*  65 */     this.id = id;
   }


   public int getItemid() {
/*  70 */     return this.itemid;
   }


   public void setItemid(int itemid) {
/*  75 */     this.itemid = itemid;
   }


   public int getQuantity() {
/*  80 */     return this.quantity;
   }


   public void setQuantity(int quantity) {
/*  85 */     this.quantity = quantity;
   }


   public int getUnk() {
/*  90 */     return this.unk;
   }


   public void setUnk(int unk) {
/*  95 */     this.unk = unk;
   }


   public int getPeriod() {
/* 100 */     return this.period;
   }


   public void setPeriod(int period) {
/* 105 */     this.period = period;
   }


   public Timestamp getStartTime() {
/* 110 */     return this.startTime;
   }


   public void setStartTime(Timestamp startTime) {
/* 115 */     this.startTime = startTime;
   }


   public Timestamp getEndTime() {
/* 120 */     return this.endTime;
   }


   public void setEndTime(Timestamp endTime) {
/* 125 */     this.endTime = endTime;
   }

   public String getMsg() {
/* 129 */     return this.msg;
   }

   public List<StarReward> getStarRewardInfo(int characterid) {
/* 133 */     List<StarReward> starRewards = new LinkedList<>();
/* 134 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 135 */         PreparedStatement ps = con.prepareStatement("SELECT * FROM star_reward WHERE characterid = ? and status = 0 and endtime > now()")) {
/* 136 */       ps.setInt(1, characterid);
/* 137 */       try (ResultSet rs = ps.executeQuery()) {
/* 138 */         while (rs.next())
         {







/* 147 */           StarReward reward = new StarReward(rs.getInt("id"), rs.getInt("itemid"), rs.getInt("quantity"), 0, rs.getInt("period"), rs.getTimestamp("starttime"), rs.getTimestamp("endtime"), rs.getString("msg"));


/* 150 */           starRewards.add(reward);
         }

       }
/* 154 */     } catch (SQLException e) {
/* 155 */       log.error("查询星星奖励失败.", e.getMessage());
     }

/* 158 */     return starRewards;
   }

   public void addStarRewardInfo(MapleCharacter chr, StarReward starReward) {
/* 162 */     boolean success = false;
/* 163 */     int addId = 0;
/* 164 */     List<StarReward> rewardList = new LinkedList<>();

/* 166 */     rewardList = getStarRewardInfo(chr.getId());

/* 168 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 169 */         PreparedStatement ps = con.prepareStatement("INSERT INTO star_reward (characterid, starttime, endtime,itemid,quantity,period,msg) VALUES (?, ?, ?, ?, ?, ?, ?)", 1)) {
/* 170 */       ps.setInt(1, chr.getId());
/* 171 */       ps.setTimestamp(2, starReward.startTime);
/* 172 */       ps.setTimestamp(3, starReward.endTime);
/* 173 */       ps.setInt(4, starReward.itemid);
/* 174 */       ps.setInt(5, starReward.quantity);
/* 175 */       ps.setInt(6, starReward.period);
/* 176 */       ps.setString(7, starReward.msg);
/* 177 */       ps.executeUpdate();
/* 178 */       ResultSet generatedKeys = ps.getGeneratedKeys();
/* 179 */       if (generatedKeys.next()) {
/* 180 */         addId = generatedKeys.getInt(1);
       }
/* 182 */       success = true;
     }
/* 184 */     catch (SQLException e) {
/* 185 */       log.error("插入星星奖励失败", e.getMessage());
     }

/* 188 */     if (success) {

/* 190 */       starReward.id = addId;
/* 191 */       rewardList.add(starReward);
/* 192 */       chr.send(RewardPacket.StarRewardInfo(rewardList));
     }
   }

   public void updateStarRewardInfo(int id, byte op) {
/* 197 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 198 */         PreparedStatement ps = con.prepareStatement("UPDATE star_reward set status= ?, gaintime = ? where id = ?")) {
/* 199 */       ps.setInt(1, op);
/* 200 */       ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
/* 201 */       ps.setInt(3, id);
/* 202 */       ps.executeUpdate();
     }
/* 204 */     catch (SQLException e) {
/* 205 */       log.error("更新星星奖励失败", e.getMessage());
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\server\reward\StarReward.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */